/**
 * Project Name:ylbx
 * File Name:StaBxmidleDataSource.java
 * Package Name:dln.stat.impl
 * Copyright (c) 2014, All Rights Reserved.
 *
 */
package dln.stat.ds;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import dao.hb.BxMidle;
import dln.stat.BxmidleCatalog;
import dln.stat.StaBxmidle;

/**
 * ClassName: StaBxmidleDataSource <br/>
 * Function: 报销通用
 * date: May 24, 2014 11:02:47 AM <br/>
 *
 * @author $dev
 * @version 1.0
 */
public class StaBxmidleDataSource extends AbstractStatDataSource<StaBxmidle> {
	
	private static final String TX = "退休";
	private String nameQuery;
	
	public StaBxmidleDataSource(String nameQuery) {
		this.nameQuery = nameQuery;
	}

    public StaBxmidleDataSource() {
		// TODO Auto-generated constructor stub
	}

	/**
     * @param list
     * @return
     */
	protected List<StaBxmidle> listProcesser(List<?> origin) {
		List<StaBxmidle> sblist = new ArrayList<StaBxmidle>();
		if (origin != null && !origin.isEmpty()) {			
			processAndGroup(origin, sblist);
		}
		return sblist;
	}

	protected void processAndGroup(List<?> origin, List<StaBxmidle> sblist) {
		Object lastGroup = null;
		Map<String, BxMidle> tempMap = new HashMap<String, BxMidle>();
		int len = origin.size();
		for (int i = 0; i < len; i++) {				
			BxMidle b = new BxMidle();
			Object group = preProcess(origin.get(i), b);
			
			boolean changed = groupUnEquals(lastGroup, group);
			if (changed) {
				preProcess(tempMap);
				StaBxmidle sb = new StaBxmidle();
				sb.setKey(lastGroup);
				sb.setUserBxMap(tempMap);
				sblist.add(sb);
				tempMap = new HashMap<String, BxMidle>();
				lastGroup = group;
			}
			boolean last = (i==len-1);
			if (last) {
				tempMap.put(b.getBxtype(), b);
				preProcess(tempMap);
				StaBxmidle sb = new StaBxmidle();
				sb.setKey(lastGroup);
				sb.setUserBxMap(tempMap);
				sblist.add(sb);
			} else {
				tempMap.put(b.getBxtype(), b);
				lastGroup = group;
			}
		}
	}

	protected boolean groupUnEquals(Object lastGroup, Object group) {
		return lastGroup != null && !group.equals(lastGroup);
	}

	protected Object preProcess(Object obj, BxMidle b) {
		Object group = null;
		if (obj.getClass().isArray()) {
			Object[] o = (Object[]) obj;
			group = (o[0] != null ? o[0].toString() : TX);
			b.setBxtype(o[1] != null ? o[1].toString() : "");
			b.setPayone(o[2] != null ? (Double) o[2] : 0.0);
			b.setPaytwo(o[3] != null ? (Double) o[3] : 0.0);
			b.setPayall(o[4] != null ? (Double) o[4] : 0.0);
			b.setBxSum(o[5] != null ? (Double) o[5] : 0.0);
		}
		return group;
	}

    protected void preProcess(Map<String, BxMidle> bmMap) {
    	// Default Value
        BxMidle bm = new BxMidle(0.0, 0.0, 0.0, 0.0);        
        for (BxmidleCatalog catalog : BxmidleCatalog.values()) {
        	if (!bmMap.containsKey(catalog.getCN()))
        		bmMap.put(catalog.getCN(), bm);
        }	
	}

	/**
     * @return
     */
    protected String getNameQuery() {
        return nameQuery;
    }

}
